import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Reward } from './reward.entity';
import { payStatusEnum } from './reward.enum';
import DataBase from 'src/config/database';
import { wechartAmountRewardBig, wechartConsumeAccontAmountRewardBig } from 'src/setting';
import { RewardSuccess } from '../rewardSuccess/rewardSuccess.entity';
import { Video } from '../video/video.entity';
import { IsRewardEnum } from '../video/video.enum';
import utils from 'src/common/utils';

@Injectable()
export class RewardService {
  constructor(
    @InjectRepository(Reward)
    private repository: Repository<Reward>,
    @InjectRepository(RewardSuccess)
    private rewardSuccessRepository: Repository<RewardSuccess>,
    @InjectRepository(Video)
    private videoRepository: Repository<Video>,
  ) {}

  async bigReward(video_id: number, wx_user: string): Promise<any> {
    let db: DataBase = new DataBase()
    let connection = await db.getConnection()
    const queryRunner = connection.createQueryRunner()
    await queryRunner.connect()
    // 开始事务
    await queryRunner.startTransaction()
    try {
      let orderId = "bigreward" + utils.Format(new Date(), 'yyyyMMddhhmmss') + new Date().getTime();

      let reward = new Reward()
      reward.video_id = video_id
      reward.wx_user = wx_user
      reward.unit_price = wechartAmountRewardBig
      reward.service_charge = wechartConsumeAccontAmountRewardBig
      reward.create_time = new Date()
      reward.pay_status = payStatusEnum.success
      reward.pay_order = orderId
      reward.pay_time = new Date()
      await this.repository.createQueryBuilder(null, queryRunner).insert().into(Reward).values(reward).execute()
      //悬赏成功
      let rewardSuccess = new RewardSuccess();
      rewardSuccess.pay_order = orderId
      rewardSuccess.pay_time = reward.pay_time
      rewardSuccess.service_charge = reward.service_charge
      rewardSuccess.unit_price = reward.unit_price
      rewardSuccess.video_id = video_id
      rewardSuccess.wx_user = wx_user
      await this.rewardSuccessRepository.createQueryBuilder(null, queryRunner).insert().into(RewardSuccess).values(rewardSuccess).execute()
      //修改视频为悬赏视频
      await this.videoRepository.createQueryBuilder(null, queryRunner).update({isreward: IsRewardEnum.yes, original: 0}).where({id: video_id}).execute()

      await queryRunner.commitTransaction()
      await queryRunner.release();
    } catch (err) {
      // 有错误做出回滚更改
      await queryRunner.rollbackTransaction()
      await queryRunner.release();
      return err
    }

  }

  findAll(): Promise<Reward[]> {
    return this.repository.find();
  }

  findOne(id: any): Promise<Reward> {
    return this.repository.findOne(id);
  }

  async remove(id: number): Promise<void> {
    await this.repository.delete(id);
  }

  /**
   * 查询某用户所有支付成功记录
   * @param wx_user 
   * @returns 
   */
  find_by_wx_user(wx_user: string): Promise<Reward[]> {
    return this.repository.find({where:{wx_user: wx_user, pay_status: payStatusEnum.success}})
  }

  async create(cats: Reward, wx_user: string): Promise<Reward> {
    let reward = new Reward()
    reward.video_id = cats.video_id
    reward.create_time = new Date()
    reward.unit_price = cats.unit_price
    reward.service_charge = 0
    reward.wx_user = wx_user
    return await this.repository.save(reward);
  }
}
